$import("com.joypard.platform.security.resource.ResourceField");

/**
 * 资源
 */
K.defDI("com.joypard.platform.security.resource.ResourceDI", {
	statics : {
		urls : {
			updateRes : $path + "/security/resource/updateResource.do",
			addRes : $path + "/security/resource/addResource.do",
			delRes : $path + "/security/resource/delResource.do",
			getChildrens : $path + "/security/resource/getChildrens.do",
			getAppResRoot : $path + "/security/resource/getAppSystemResourceRoot.do",
			getResRoot : $path + "/security/resource/getResourceRoot.do",
			resView : $path + "/security/resource/resourceView.do"
		}

		,
		/**
		 * 覆盖创建节点方法
		 */
		createNode : function(attr) { // mapping node
			attr.text = attr.name || attr.text;
			attr.children = attr.childrenRess || attr.children;
			attr.iconCls = attr.iconCls || "res_icon_" + attr.type;
			attr.leaf = attr.leaf == 1;
			attr.expanded = attr.expand == 1;
			return K.Ext.tree.TreeLoader.prototype.createNode.call(this, attr);
		}

		,
		/**
		 * 创建一个资源树加载器
		 */
		createResTreeLoader : function() {
			var options = {
				url : this.urls.getChildrens,
				requestMethod : "GET",
				baseParams : {
					parentResId : 0
				},
				listeners : {
					beforeload : function(loader, node) {
						this.baseParams.parentResId = node.attributes.id;
					}
				},
				createNode : this.createNode
			};
			return new K.Ext.tree.TreeLoader(options);
		},
		/**
		 * 同步创建根节点
		 */
		createResRoot : function(resId) {
			var root = null;
			K.syncAjax({
				params : {
					resId : resId || ""
				},
				url : this.urls.getResRoot,
				success : function(text) {
					var output = K.output(text);
					root = output.getParameter("resource");
				}
			});
			root.checked = false;
			root.text = root.name;
			root.leaf = root.leaf == 1;
			root.iconCls = root.iconCls || "res_icon_Root";
			return root;
		}

		,
		/**
		 * 同步创建应用根节点
		 */
		createAppResRoot : function(appId) {
			var root = null;
			K.syncAjax({
				url : this.urls.getAppResRoot,
				params : {
					appId : appId
				},
				success : function(text) {
					var output = K.output(text);
					root = output.getParameter("resource");
				}
			});
			if (root) { // 有可能不存在任何资源
				root.text = root.name;
				root.leaf = root.leaf == 1;
				root.iconCls = root.iconCls || "res_icon_Root";
			}
			return root;
		}

		,
		/**
		 * 创建异步资源根节点
		 */
		createAsyncRootNode : function(resId) {
			var root = this.createResRoot(resId);
			return new Ext.tree.AsyncTreeNode(root);
		}

		,
		/**
		 * 创建异步系统资源根节点
		 */
		createAsyncAppRootNode : function(appId) {
			var root = this.createAppResRoot(appId);
			return root ? new Ext.tree.AsyncTreeNode(root) : root;
		}

		,
		/**
		 * 创建获取子资源的数据存储器
		 */
		createResChildrensStore : function() {
			var options = {
				url : this.urls.getChildrens,
				fields : ResourceField.fields
			};
			return new K.Ext.data.JsonStore(options);
		}

		,
		createResTreeGridLoader : function() {
			var options = {
				url : ResourceDI.urls.getChildrens,
				requestMethod : "GET",
				baseParams : {
					parentResId : 0
				},
				listeners : {
					beforeload : function(loader, node) {
						this.baseParams.parentResId = node.id;
					}
				},
				createNode : function(attr) {
					attr.text = attr.name;
					return Ext.ux.tree.TreeGridLoader.prototype.createNode.call(this, attr);
				}
			};
			return new Ext.ux.tree.TreeGridLoader(options);
		}
	}
});